//
//  MBHTTPTool.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2018/1/4.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBHTTPTool.h"

@interface MBHTTPTool ()
@property (nonatomic,strong) AFHTTPSessionManager * mgr;
@end

@implementation MBHTTPTool

static MBHTTPTool* httpTool;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        httpTool = [super allocWithZone:zone];
    });
    return httpTool;
}

+ (instancetype)sharedInstance
{
    return [[self alloc]init];
}

- (id)copy
{
    return [[[self class]alloc]init];
}


#pragma mark - lazy load
- (AFHTTPSessionManager *)mgr
{
    if(_mgr == nil){
        _mgr =[AFHTTPSessionManager manager];
        _mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
        _mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
        _mgr.requestSerializer.timeoutInterval = 10;
//        [_mgr.responseSerializer setStringEncoding:NSUTF8StringEncoding];
        _mgr.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html" , nil];
        [_mgr.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [_mgr.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"];
    }
    return _mgr;
}

//GET请求
- (void)GET:(NSString *)url params:(NSDictionary *)params success:(MBRequestSuccess)success failure:(MBRequestFailure)failure
{
    [self.mgr GET:[NSString stringWithFormat:@"%@%@",BASE_URL,url] parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MBHTTPResponse* response = [[MBHTTPResponse alloc]initWithJsonData:responseObject];
        if ([response.msg containsString:@"token"]) {
            [InterfaceHUD showErrorHUDWithString:response.msg];
            [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION_LOGIN_SHOW_VIEW object:nil];
            return ;
        }
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

//POST请求
- (void)POST:(NSString *)url params:(NSDictionary *)params success:(MBRequestSuccess)success failure:(MBRequestFailure)failure
{
    [self.mgr POST:[NSString stringWithFormat:@"%@%@",BASE_URL,url] parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MBHTTPResponse* response = [[MBHTTPResponse alloc]initWithJsonData:responseObject];
        if ([response.msg containsString:@"token"]) {
            [InterfaceHUD showErrorHUDWithString:response.msg];
            [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION_LOGIN_SHOW_VIEW object:nil];
            return ;
        }
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

//POST请求(上传文件)
- (void)POST:(NSString *)url params:(NSDictionary *)params withFileData:(NSData *)data andFileParamName:(NSString *)fileParamName success:(MBRequestSuccess)success failure:(MBRequestFailure)failure
{
    [self.mgr POST:[NSString stringWithFormat:@"%@%@",BASE_URL,url] parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSString * fileName = [NSString stringWithFormat:@"%@.jpg",[MBUtil uuid]];
        //添加上传数据
        [formData appendPartWithHeaders:@{} body:data];
        [formData appendPartWithFileData:data name:fileParamName fileName:fileName mimeType:@"image/jpeg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}


@end
